//
//  WHZSportsHomeTracking.m
//  WHZGuDong
//
//  Created by whz on 16/11/8.
//  Copyright © 2016年 whz. All rights reserved.
//

#import "WHZSportsHomeTracking.h"

@implementation WHZSportsHomeTracking
{
    // 起始点位置
    CLLocation *_startLocation;
    // 所有运动线条模型的数组
    NSMutableArray<WHZSportTrackingLine *> *_trackingLines;
}

- (instancetype)initWithType:(WHZSportsType)type state:(WHZSportsState)state{
    
    if (self = [super init]) {
        
        _sportType = type;
        _sportsState = state;
        
        // 实例化数组
        _trackingLines = [NSMutableArray array];
    }
    return self;
}

#pragma mark - 用户主动暂停 设置数据
- (void)setSportsState:(WHZSportsState)sportsState {

    // MARK: - 如果不用成员变量的话，则永远是continue，需要从成员变量里取值
    _sportsState = sportsState;
    
    // MARK: - 如果不是继续 需要清空起始点
    if (_sportsState != WHZSportsStateContinue) {
        _startLocation = nil;
    }
}

#pragma mark - 计算型属性 & 公共方法
- (UIImage *)sportImage {
    
    UIImage *image;
    
    switch (_sportType) {
            
        case WHZSportsTypeRun:
            image = [UIImage imageNamed:@"map_annotation_run"];
            break;
        case WHZSportsTypeWalk:
            image = [UIImage imageNamed:@"map_annotation_walk"];
            break;
        case WHZSportsTypeBike:
            image = [UIImage imageNamed:@"map_annotation_bike"];
            break;
    }
    return image;
}

- (WHZSportPolyline *)appendLocation:(CLLocation *)location {
    
    // MARK: - 测试时间戳
    //    NSLog(@"%f",[[NSDate date] timeIntervalSinceDate:location.timestamp]);
    
    // MARK: - 判断速度是否发生变化 - 性能优化 (位置不动的时候虽然看不到动，但是会一直在绘制) 室内定位，速度有可能为 负数
    if (location.speed <= 0) {
        
        return nil;
    }
    
    // MARK: - 判断定位的时间差值，暂时定义一个时间差值银子 - 性能优化 (避免室内出现杂线)
    CGFloat factor = 2;
    if ([[NSDate date] timeIntervalSinceDate:location.timestamp] > factor) {
        
        // 如果超过时间的差值，就认为定位的精度不够
        return nil;
    }
    
    // MARK: - 判断是否存在起始点
    if (_startLocation == nil) {
        
        _startLocation = location;
        
        return nil;
    }
    
    // MARK: - 追踪线条模型
    WHZSportTrackingLine *trackingLine = [[WHZSportTrackingLine alloc] initWithStartLocation:_startLocation endLocation:location];
    
    [_trackingLines addObject:trackingLine];
    
    // MARK: - 测试 总时长 - KVC - sum 能够计算数组中的汇总数据，返回类型是 NSNumber
    //    NSLog(@"%@ -- %@",[_trackingLines valueForKeyPath:@"time"], [[_trackingLines valueForKeyPath:@"@sum.time"] class]);
    NSLog(@"!!! %f -- %f -- %f -- %f",self.avgSpeed, self.maxSpeed, self.totalTime, self.totalDistance);
    
    // MARK: - 将当前位置设置成下一次起始点
    _startLocation = location;
    
    // MARK: - 返回折现模型
    return trackingLine.polyline;
}

#pragma mark - KVC设赋值 懒加载
- (double)totalTime {
    
    return [[_trackingLines valueForKeyPath:@"@sum.time"] doubleValue];
}
- (double)avgSpeed {
    
    return [[_trackingLines valueForKeyPath:@"@avg.speed"] doubleValue];
}
- (double)maxSpeed {
    
    return [[_trackingLines valueForKeyPath:@"@max.speed"] doubleValue];
}
- (double)totalDistance {

    return [[_trackingLines valueForKeyPath:@"@sum.distance"] doubleValue];
}

@end
